package problema037;

public class TruncatablePrimes {
	
	public static void main(String[] args){
		System.out.println(truncablePrimes());
	}
	
	public static long truncablePrimes(){
		long res = 0;
		for(double i = 10; i < 1000000; i++){
			if(esPrimo(i)){
				if(esPrimoIzq(i)){
					if(esPrimoDer(i)){
						res += (long) i;
					}
				}
			}
		}	
		return res;
	}
	
	public static boolean esPrimoIzq(double i){
		long n = (long) i, pos = 10;
		while((n / pos) != 0){
			if(!esPrimo(n%pos)) return false;
			pos *= 10;
		}
		return true;
	}
	
	public static boolean esPrimoDer(double i){
		long n = (long) i;
		String s = Long.toString(n);
		long pos = (long) Math.pow(10,(double)s.length()-1);
		while(pos != 1){
			if(!esPrimo(n/pos)) return false;
			pos /= 10;
		}
		return true;
	}
	
	public static boolean esPrimo(double num){
		if(num < 2) return false;
		double hasta = Math.sqrt(num);
		int hastaI = (int) hasta;
		for(int i = 2; i <= hastaI; i++){
			if(num % i == 0) return false;
		}
		return true;
	}

}
